package org.jeecg.modules.ywPalletInventory.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.constant.CommonConstant;
import org.jeecg.constant.InventoryConstant;
import org.jeecg.modules.ywPalletInventory.entity.YwPalletInventoryInfo;
import org.jeecg.modules.ywPalletInventory.mapper.YwPalletInventoryInfoMapper;
import org.jeecg.modules.ywPalletInventory.service.IYwPalletInventoryInfoService;
import org.jeecg.modules.ywPalletOut.entity.YwPalletOutDetail;
import org.jeecg.modules.ywPalletOut.entity.YwPalletOutMain;
import org.jeecg.modules.ywPalletOut.service.IYwPalletOutDetailService;
import org.jeecg.modules.ywPalletOut.service.IYwPalletOutMainService;
import org.jeecg.modules.ywSemiFinishedProductOut.entity.YwSemiFinishedProductOutDetail;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 托盘库存
 * @Author: jeecg-boot
 * @Date: 2024-08-27
 * @Version: V1.0
 */
@Service
public class YwPalletInventoryInfoServiceImpl extends ServiceImpl<YwPalletInventoryInfoMapper, YwPalletInventoryInfo> implements IYwPalletInventoryInfoService {

    @Resource
    private YwPalletInventoryInfoMapper ywPalletInventoryInfoMapper;
    @Resource
    private IYwPalletOutMainService ywPalletOutMainService;
    @Resource
    private IYwPalletOutDetailService ywPalletOutDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outBatch(List<String> wantOutIdList) {
        List<YwPalletInventoryInfo> inventoryInfoList = this.ywPalletInventoryInfoMapper.selectBatchIds(wantOutIdList);
        Assert.isTrue(inventoryInfoList.size() == wantOutIdList.size(), "出库失败,请检查数据是否异常！");

        YwPalletOutMain outMain = new YwPalletOutMain();
        outMain.setOutDate(new Date());
        outMain.setOutStatus(InventoryConstant.OutStatusEnum.OUT_END.getCode());
        ywPalletOutMainService.save(outMain);

        List<YwPalletOutDetail> outDetailList = inventoryInfoList.stream().map(data -> {
            YwPalletOutDetail outDetail = new YwPalletOutDetail();
            outDetail.setOutId(outMain.getId());
            outDetail.setPalletDept(data.getPalletDept());
            outDetail.setPalletVolume(data.getPalletVolume());
            outDetail.setOutNum(data.getInventoryNum());
            outDetail.setInventoryId(data.getId());
            outDetail.setInventoryNum(data.getInventoryNum());
            outDetail.setOutVolume(data.getInventoryVolume());
            outDetail.setStatus(CommonConstant.StatusEnum.YES.getCode());

            data.setInventoryNum(0);
            return outDetail;
        }).collect(Collectors.toList());
        ywPalletOutDetailService.saveBatch(outDetailList);

        this.updateBatchById(inventoryInfoList);
    }
}
